package com.ndp.fb.scheduler.job.statistics;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.config.ConfigCentre;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.data.api.service.schedulerservice.statitics.FetchGaDataJobService;
import com.ndp.fb.rdb.api.ProductManageRdbService;
import com.ndp.fb.rdb.model.productmanage.ProductManage;
import com.ndp.fb.scheduler.annotation.CronScheduled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by hexin on 2016/6/16.
 */
@Component
public class FetchGaDataJob {
    private static Logger logger = LoggerFactory.getLogger(FetchGaDataJob.class);

    @Reference
    FetchGaDataJobService fetchGaDataService;

    @Reference
    private ProductManageRdbService productManageRdbService;

    @CronScheduled(cronkey = ConfigConst.SCHEDULE_FETCH_GA_STATS_CRON, enablekey = ConfigConst.SCHEDULE_FETCH_GA_STATS_ENABLE)
    public void fetchGaDataByDate() {
        //每小时跑一次。值执行当前是凌晨的时区.
        Integer integer = ConfigCentre.getInteger(ConfigConst.SCHEDULE_FETCH_GA_STATS_OFFSET_DAY);
        if (integer == 0) {
            integer = -1;
        } else {
            integer = integer * -1;
        }

        List<ProductManage> pms = productManageRdbService.findAll();
        for (ProductManage productManage : pms) {
            fetchGaDataService.fetchGaDataByDate(productManage, integer);
        }
    }
}
